package crawler

import (
	"bytes"

	"github.com/PuerkitoBio/goquery"
)

/**
Html解析，借助于goquery
*/
type (
	ResultItem  map[string]string
	ResultItems []ResultItem
)

//SelectList Html解析，内容选择
func SelectList(bs ByteContent, /*网页内容 UTF8编码*/
	selector string, //选择器
	projects []string /*元素属性清单*/) (ResultItems, error) {
	doc, err := goquery.NewDocumentFromReader(bytes.NewReader(bs))
	if err != nil {
		return nil, err
	}
	ret := make(ResultItems, 0)
	doc.Find(selector).Each(func(i int, s *goquery.Selection) {
		item := make(ResultItem)
		for _, k := range projects {
			v := s.AttrOr(k, "")
			item[k] = v
		}
		html, _ := s.Html()
		item["text"] = s.Text()
		item["html"] = html
		ret = append(ret, item)
	})
	return ret, nil
}
